package com.lh.oss.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            //加了ApiOperation注解的类，才生成接口文档
            .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
            //包下的类，才生成接口文档
            //.apis(RequestHandlerSelectors.basePackage("com.lh.web.controller"))
            .paths(PathSelectors.any())
            .build()
            .securitySchemes(security());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("新闻头条OSS接口")
            .description("世界头条-OSS文档及处理状态码code值说明：</br>"
                    + "0 -- 操作成功</br>"
                    + "1000 -- 参数为空或非法</br>"
                    + "1001 -- 添加失败</br>"
                    + "1002 -- 更新失败</br>"
                    + "1003 -- 删除失败</br>"
                    + "1004 -- 操作失败</br>"
                    + "1005 -- 已存在</br>"
                    + "1006 -- 密码错误</br>"
                    + "1007 -- 用户被锁定或者删除</br>"
                    + "1008 -- 限制重复提交</br>"
                    + "1009 -- 数据不存在</br>"
                    + "1010 -- 两次输入不一样</br>"
                    + "1011 -- 验证码错误</br>"
                    + "1012 -- 无效</br>"
                    + "9110 -- 非法操作</br>"
                    + "9111 -- token签名与本地计算签名不匹配</br>"
                    + "9112 -- token过期</br>"
                    + "9990 -- 系统异常</br>"
                    + "9999 -- 未知异常</br>")
            .version("1.0.0")
            .build();
    }

    private List<ApiKey> security() {
        return newArrayList(
            new ApiKey("token", "token", "header")
        );
    }

}